package gf.client.controller;

import gf.client.utils.HttpUtils;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;

/**
 * 文件下载
 *
 * @author Gaofeng
 * @since 2021/10/17 15:57
 **/

public class DownloadController {

    public static String downFile(String uuid, String downPath) throws Exception {
        HttpURLConnection connection = HttpUtils.creatUtil("http://127.0.0.1:8080/file/download", "GET");
        connection.setRequestProperty("uuid", uuid);
        connection.connect();
        if (connection.getResponseCode() != 200) {
            return "异常状态码" + connection.getResponseCode();
        } else {
            InputStream in = connection.getInputStream();
            String fileType = connection.getHeaderField("filetype");
            int length = in.available();
            OutputStream out = new FileOutputStream("C:\\Download\\" + uuid + fileType);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buf)) > 0) {
                out.write(buf, 0, bytesRead);
            }
            return "成功下载文件";
        }
    }
}
